package com.woniuxy;

import javax.swing.*;
import java.util.*;

/**
 * @author 作者
 * @create 2025-09-12 16:19
 */
public class Test03 {
    public static void main(String[] args) {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 12);
        //班级集合，使用Map key(学生学号)，value(学生对象)
        Map<String, Student> class1 = new HashMap();
        class1.put("1", s1);
        class1.put("2", s2);

        Student s3 = new Student(1, "ww", 13);
        Student s4 = new Student(2, "zl", 12);
        Map<String, Student> class2 = new HashMap();
        class2.put("1", s3);
        class2.put("2", s4);

        //学校
        Map<String, Map<String, Student>> school = new HashMap<>();
        school.put("001", class1);
        school.put("002", class2);
        System.out.println(school);
        //获取所有key的集合,就是班级编号
        Set<String> classNums = school.keySet();
        for (String classNum : classNums) {
            System.out.println(classNum);
            //通过班级的key，获取班级中所有学生的集合
            Map<String, Student> students = school.get(classNum);
            Set<String> studentNums = students.keySet();
            for (String studentNum : studentNums) {
                System.out.println(students.get(studentNum));
            }
        }
    }

    private static void test03() {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 12);
        List<Student> class1 = new ArrayList<Student>();
        class1.add(s1);
        class1.add(s2);

        Student s3 = new Student(1, "ww", 13);
        Student s4 = new Student(2, "zl", 12);
        List<Student> class2 = new ArrayList<>();
        class2.add(s3);
        class2.add(s4);

        Map<String, List<Student>> school = new HashMap<>();
        school.put("001", class1);
        school.put("002", class2);

        //遍历学校，所有班，所有学生
        //Map.Entry<班级编号, 班级所有学生的List集合>
        Set<Map.Entry<String, List<Student>>> classes = school.entrySet();
        for (Map.Entry<String, List<Student>> myclass : classes) {
            System.out.println("key就是班级编号:" + myclass.getKey());
            List<Student> students = myclass.getValue();
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    private static void test02() {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 12);
        //班级集合，使用Map key(学生学号)，value(学生对象)
        Map<String, Student> class1 = new HashMap();
        class1.put("1", s1);
        class1.put("2", s2);

        Student s3 = new Student(1, "ww", 13);
        Student s4 = new Student(2, "zl", 12);
        Map<String, Student> class2 = new HashMap();
        class2.put("1", s3);
        class2.put("2", s4);

        List<Map<String, Student>> school = new ArrayList();
        school.add(class1);
        school.add(class2);
        //遍历显示，每个班级和每个班的学生
        for (int i = 0; i < school.size(); i++) {
            System.out.println("进入一个班");
            Map<String, Student> myclass = school.get(i);
            Set<String> keys = myclass.keySet();
            for (String key : keys) {
                System.out.println(key);
                System.out.println(myclass.get(key));
                System.out.println("------------");
            }
        }


        //遍历显示，每个班级和每个班的学生
//        for(Map<String,Student> myclass  :school){
//            System.out.println("进入一个班");
//            Set<Map.Entry<String, Student>> entries = myclass.entrySet();
//            for(Map.Entry<String, Student> student :  entries){
//                System.out.println(student.getKey());
//                System.out.println(student.getValue());
//                System.out.println("---------------------");
//            }
//        }
    }

    private static void test01() {
        //两个学生
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 12);
        //学生集合：一个班
        List<Student> class1 = new ArrayList<Student>();
        //一班有两个学生
        class1.add(s1);
        class1.add(s2);

        //第二个班有两个学生
        Student s3 = new Student(1, "ww", 13);
        Student s4 = new Student(2, "zl", 12);
        List<Student> class2 = new ArrayList<>();
        class2.add(s3);
        class2.add(s4);

        //一个学校，学校有若干班：学校就是班级的集合
        List<List<Student>> school = new ArrayList<>();
        //给学校添加班级
        school.add(class1);
        school.add(class2);

        //遍历显示，每个班级和每个班的学生
        for (List<Student> myclass : school) {
            System.out.println("进入一个班");
            for (Student student : myclass) {
                System.out.println(student);
            }
        }
    }
}
